package com.tdam2011.grupo05.ui.activities.contactos;

import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.tdam2011.grupo05.R;
import com.tdam2011.grupo05.entities.contact.Contact;
import com.tdam2011.grupo05.entities.logs.LogImp;
import com.tdam2011.grupo05.services.ServiceHelper;

public class ContactoDetailFragment extends Fragment {

	private ContactNumberAdapter adapterPhone;
	private ContactNumberAdapter adapterMail;

	private Contact contacto;
	private Context context;

	private TextView textViewContactName;
	private ListView lv1 = null;
	private ListView lv2 = null;

	private String mail;
	private String phone;

	LogImp accion;

	@Override
	public void onAttach(Activity activity) {

		super.onAttach(activity);

		context = activity;

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.fragment_contacto_detail,
				container, false);
		textViewContactName = (TextView) view.findViewById(R.id.texto_contacto);
		lv1 = (ListView) view.findViewById(R.id.lista_tel);
		lv2 = (ListView) view.findViewById(R.id.lista_email);

		textViewContactName.setText(contacto.getNombre());

		adapterPhone = new ContactNumberAdapter(contacto.getPhone(), true);
		adapterMail = new ContactNumberAdapter(contacto.getEmail(), false);

		lv1.setAdapter(adapterPhone);
		lv2.setAdapter(adapterMail);

		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	public void setContacto(Contact contact) {
		contacto = contact;
	}

	// ////////////////HOLDER OBJECT/////////////////////
	class Holder {
		TextView txtContactNumber;
		ImageView buttonMail;
		ImageView buttonSms;
		ImageView buttonCall;
	}

	// ////////////////CUSTOM ADAPTER////////////////////
	class ContactNumberAdapter extends BaseAdapter {

		private ArrayList<String> _telefonos;
		private LayoutInflater _inflater;
		private boolean tipo;

		public ContactNumberAdapter(ArrayList<String> telefonos, boolean tipo) {
			this.tipo = tipo;
			this._telefonos = telefonos;
			_inflater = LayoutInflater.from(context);
		}

		public void setTelefonos(ArrayList<String> telefonos) {
			_telefonos = telefonos;
		}

		public String getTelefono(int position) {
			String telefono = (String) getItem(position);
			return telefono;
		}

		@Override
		public int getCount() {
			return _telefonos.size();
		}

		@Override
		public String getItem(int arg0) {
			return _telefonos.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Holder holder = null;
			if (convertView == null) {
				convertView = _inflater.inflate(R.layout.list_item_telefono,
						null);
				holder = new Holder();
				holder.txtContactNumber = (TextView) convertView
						.findViewById(R.id.numero_telefono);

				holder.buttonSms = (ImageView) convertView
						.findViewById(R.id.image_det_sms);
				holder.buttonCall = (ImageView) convertView
						.findViewById(R.id.image_det_call);
				holder.buttonMail = (ImageView) convertView
						.findViewById(R.id.image_det_mail);

				convertView.setTag(holder);
			} else {
				holder = (Holder) convertView.getTag();
			}

			String telefono = _telefonos.get(position);

			// holder.iconContact.setImageResource(R.drawable.ic_contact_item2);
			holder.txtContactNumber.setText(telefono);
			holder.buttonSms.setOnClickListener(smsClickListener);
			holder.buttonCall.setOnClickListener(callClickListener);
			holder.buttonMail.setOnClickListener(emailClickListener);

			if (tipo) {
				holder.buttonMail.setVisibility(View.GONE);
				holder.buttonSms.setVisibility(View.VISIBLE);
				holder.buttonCall.setVisibility(View.VISIBLE);
			} else {
				holder.buttonMail.setVisibility(View.VISIBLE);
				holder.buttonSms.setVisibility(View.GONE);
				holder.buttonCall.setVisibility(View.GONE);
			}

			return convertView;
		}

		private OnClickListener smsClickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				int position = lv1.getPositionForView((View) v.getParent());
				Intent smsIntent = new Intent(Intent.ACTION_VIEW);
				phone = contacto.getPhone().get(position);
				smsIntent.putExtra("address", phone);
				smsIntent.setType("vnd.android-dir/mms-sms");
				startActivityForResult(smsIntent, 200);

			}
		};

		private OnClickListener callClickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				int position = lv1.getPositionForView((View) v.getParent());
				Intent callIntent = new Intent(Intent.ACTION_CALL);
				phone = contacto.getPhone().get(position);
				callIntent.setData(Uri.parse("tel:"
						+ contacto.getPhone().get(position)));
				startActivityForResult(callIntent, 400);
			}
		};

		private OnClickListener emailClickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				int position = lv2.getPositionForView((View) v.getParent());
				final Intent sendIntent = new Intent(
						android.content.Intent.ACTION_SEND);
				sendIntent.setType("text/plain");
				mail = contacto.getEmail().get(position);
				sendIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
						new String[] { mail });
				sendIntent.putExtra("subject", "(Sin especificar)");
				sendIntent.putExtra(Intent.EXTRA_TEXT, "");
				startActivityForResult(
						Intent.createChooser(sendIntent, "Enviar e-mail"), 600);
			}
		};
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		accion = new LogImp();
		accion.setNombreContacto(contacto.getNombre());
		accion.setFecha(new Date());
				
		switch (requestCode) {
		case 200:
			accion.setTipo(R.drawable.ic_sms_log_list_outgoing_sms);
			accion.setData(phone);
			ServiceHelper.getIntance(getActivity()).getDataManager()
					.saveLog(accion);
			break;

		case 400:
			accion.setTipo(R.drawable.ic_call_log_list_outgoing_call);
			accion.setData(phone);
			//if(accion.getData()!="")
			ServiceHelper.getIntance(getActivity()).getDataManager()
					.saveLog(accion);
			break;

		case 600:
			accion.setTipo(R.drawable.ic_mms_log_list_outgoing_mms);
			accion.setData(mail);
			ServiceHelper.getIntance(getActivity()).getDataManager()
			.saveLog(accion);
			break;

		}

	}

}
